package com.ss6621.spring1.Service.impl;

import com.ss6621.spring1.Mapper.UserMapper;
import com.ss6621.spring1.pojo.CustomUserDetails;
import com.ss6621.spring1.pojo.User;
import jakarta.annotation.Resource;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service

public class UserDetailsServiceImpl implements UserDetailsService {
   @Resource
    private  UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.findByUsername(username);
        if (user == null) throw new UsernameNotFoundException("用户不存在");


        // 手动创建权限（根据你的 role 字段）
        GrantedAuthority authority = new SimpleGrantedAuthority(user.getRole());

        //  返回自定义的 CustomUserDetails，包含 id！
        return new CustomUserDetails(user, Arrays.asList(authority));
    }
}
